package com.gl.emms.whale.app;
import java.util.LinkedList;
import java.util.List;

import com.gl.emms.client.android.EMMSPushManager;
import com.gl.emms.whale.bean.UserData;
import com.gl.emms.whale.util.HeartBeatInfoSaveUtil;

import android.app.Activity;
import android.app.Application;

/**单例 Application
 * @author ganlinux
 *
 */
public class MyApplication extends Application {
	//存放所有的activity，以便释放
	private List<Activity> mList = new LinkedList<Activity>();
	//单例模式
	private static MyApplication instance ;
	//用户数据类
	private static UserData userData ;
	public synchronized  static MyApplication getInstance() {
		if(instance==null){
			instance = new  MyApplication();
		}
		return instance;
	}
	public static UserData getUserData() {
		if(userData==null){
			userData = new  UserData();
		}
		return userData;
	}
	public void onCreate(){
		getInstance();
		getUserData();
		userData.setName("");
		userData.setIsOnline(false);
		userData.setIdIp("0");
		userData.setId("0");
		//初始化心跳 以后可以删除
		new HeartBeatInfoSaveUtil(this).setHeartBeatInfo(userData.getIdIP(),userData.getIsOnline(), userData.getName());
		
		super.onCreate();  
	}
	// add Activity  
	public void addActivity(Activity activity) { 
		mList.add(activity); 
	} 

	public void exit() {
		try { 
			for (Activity activity:mList) { 
				if (activity != null) 

					activity.finish(); 
			} 
		} catch (Exception e) { 
			e.printStackTrace(); 
		} finally { 
			System.exit(0); 
		} 
	}
	@Override
	public void onLowMemory() { 
		super.onLowMemory();     
		System.gc(); 
	} 
	@Override
	public void onTerminate() {
	super.onTerminate();
	EMMSPushManager.destory(this);
	exit();
	}
}
